package com.itheima.easy.adapter.impl;

import com.itheima.easy.adapter.WapPayAdapter;
import com.itheima.easy.constant.TradingConstant;
import com.itheima.easy.handler.WapPayHandler;
import com.itheima.easy.utils.RegisterBeanHandler;
import com.itheima.easy.vo.TradingVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WapPayAdapter.java
 * @Description 手机网页支付适配实现
 */
@Slf4j
@Component
public class WapPayAdapterImpl implements WapPayAdapter {

    @Autowired
    RegisterBeanHandler registerBeanHandler;

    @Autowired
    RedissonClient redissonClient;

    private static Map<String,String> wapPayHandlers =new HashMap<>();

    static {
        wapPayHandlers.put(TradingConstant.TRADING_CHANNEL_ALI_PAY,"aliWapPayHandler");
        wapPayHandlers.put(TradingConstant.TRADING_CHANNEL_WECHAT_PAY,"wechatWapPayHandler");
    }

    /***
     * @description 生成交易表单，渲染后自动跳转支付宝或者微信引导用户完成支付
     * @param tradingVo 订单单
     * @return TradingVo对象中的placeOrderJson：支付宝为form表单字符串、微信为prepay_id标识
     */
    public TradingVo wapTrading(TradingVo tradingVo) {
        //根据渠道获得bean名称
        String wapPayHandlerString = wapPayHandlers.get(tradingVo.getTradingChannel());

        //从IOC容器中找到WapPayHandler实现
        WapPayHandler wapPayHandler = registerBeanHandler.getBean(wapPayHandlerString, WapPayHandler.class);

        //wapTrading支付交易处理
        TradingVo tradingVoResult = wapPayHandler.wapTrading(tradingVo);
        return tradingVoResult;
    }
}
